package top.jiangqiang.qianyi.business.service.impl;

import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import top.jiangqiang.qianyi.business.model.request.*;
import top.jiangqiang.qianyi.business.model.response.AdminUserDetailResponse;
import top.jiangqiang.qianyi.business.model.response.AdminUserListResponse;
import top.jiangqiang.qianyi.business.model.response.AdminUserPageResponse;
import top.jiangqiang.qianyi.business.service.AdminUserService;
import top.jiangqiang.qianyi.common.constants.auth.Platform;
import top.jiangqiang.qianyi.common.auth.service.AuthService;
import top.jiangqiang.qianyi.common.base.model.response.PageResult;
import top.jiangqiang.qianyi.common.base.model.response.Result;
import top.jiangqiang.qianyi.common.base.service.impl.BaseServiceImpl;
import top.jiangqiang.qianyi.common.constants.http.Error400Status;
import top.jiangqiang.qianyi.common.exception.Error400Exception;
import top.jiangqiang.qianyi.data.model.entity.AdminUser;
import top.jiangqiang.qianyi.data.model.entity.UserInfo;
import top.jiangqiang.qianyi.data.service.AdminUserDbService;

import java.util.List;

/**
 * <p>
 * 后台用户账号 服务实现类
 * </p>
 *
 * @author JiangQiang
 * @since 2024-06-03
 */
@Service
@RequiredArgsConstructor
public class AdminUserServiceImpl extends BaseServiceImpl<AdminUserDbService, AdminUser> implements AdminUserService {

    private final AuthService authService;

    @Override
    public Result<PageResult<AdminUserPageResponse>> page(AdminUserPageRequest request) {
        PageResult<AdminUserPageResponse> page = request.toPage();
        return Result.ok(getBaseDbService().lambdaJoinWrapper().page(page, AdminUserPageResponse.class));
    }

    @Override
    public Result<List<AdminUserListResponse>> list(AdminUserListRequest request) {
        return Result.ok(getBaseDbService().lambdaJoinWrapper().list(AdminUserListResponse.class));
    }

    @Override
    public Result<Void> add(AdminUserSaveRequest request) {
        AdminUser adminUser = typeConverter.convert(request, AdminUser.class);
        save(adminUser);
        return Result.ok();
    }

    @Override
    public Result<Void> update(AdminUserUpdateRequest request) {
        AdminUser adminUser = typeConverter.convert(request, AdminUser.class);
        updateById(adminUser);
        return Result.ok();
    }

    @Override
    public Result<AdminUserDetailResponse> detail(Long id) {
        return Result.ok(getById(id, AdminUserDetailResponse.class));
    }

    @Override
    public Result<Void> delete(Long id) {
        removeById(id);
        return Result.ok();
    }

    @Override
    public Result<String> login(AdminUserLoginRequest loginRequest) {
        AdminUser adminUser = getBaseDbService().lambdaJoinWrapper().innerJoin(UserInfo.class, UserInfo::getId, AdminUser::getUserId)
                .eq(AdminUser::getUsername, encryptService.dbEncrypt(loginRequest.getUsername())).one();
        if (adminUser != null) {
            if (encryptService.matches(loginRequest.getPassword(), adminUser.getPassword())) {
                return Result.ok(authService.login(adminUser.getId(), Platform.ADMIN));
            }
        }
        throw new Error400Exception(Error400Status.BAD_REQUEST_INCORRECT_USERNAME_OR_PASSWORD);
    }

}